package top.szhome.framework.starter.template.engine;

import java.util.HashMap;
import java.util.Map;

/**
 * 模板引擎工厂
 *
 * @author 唐加利
 * @date 2023/7/14
 **/
public class TemplateEngineFactory {

    private static Map<TemplateEngineType, TemplateEngine> CACHE = new HashMap<>(3);


    /**
     * 根据类型获取模板引擎
     *
     * @param type
     * @return
     */
    public static TemplateEngine get(TemplateEngineType type) {
        TemplateEngine engine = CACHE.get(type);
        if(engine != null){
            return engine;
        }

        synchronized (CACHE) {
            engine = CACHE.get(type);
            if(engine != null){
                return engine;
            }

            switch (type) {
                case BEETL:
                    engine = new BeetlEngine();
                    break;
                case VELOCITY:
                    engine = new VelocityEngine();
                    break;
                default:
                    engine = new FreemarkerEngine();
            }

            CACHE.put(type, engine);
            return engine;
        }

    }

}
